<template>
	<view v-if="JSON.stringify(entrustObj)!='{}'">
		<cu-custom bgColor="bg-black" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">委托详情</block>
		</cu-custom>
		<view class="cu-card dynamic no-card" style="margin-bottom: 300upx;">
			<view class="cu-item shadow">
				<view class="cu-list menu-avatar">
					<view class="cu-item ">
						<view class="cu-avatar round lg" :style="{backgroundImage:'url('+ entrustObj.handlUrl +')'}"></view>
						<view class="content flex-sub">
							<!-- <view>凯尔</view> -->
							<view class="text-grey">
								<view class="item_name">
									{{entrustObj.nickName}}
								</view>
							</view>
							<view class="text-gray text-sm flex justify-between">
								{{entrustObj.createTime}}
							</view>
						</view>
						<!-- <view class="cu-tag bg-yellow radius" style="color: #fff;">
							打官司
						</view> -->
					</view>
				</view>
				<view class="text-content" style="margin-top: 10upx;">
					{{entrustObj.content}}
				</view>
				<view class="cu-form-group margin-tb-sm" v-if="entrustObj.imgList.length!=0">
					<view class="grid col-4 grid-square flex-sub">
						<view class="bg-img" v-for="(item,index) in entrustObj.imgList" :key="index" @tap="ViewImage" :data-url="item">
							<image :src="item" mode="aspectFill"></image>
						</view>
					</view>
				</view>
				<view class="pic_title flex solid-bottom padding justify-start align-center">
					<view class="item_name " style="margin-right: 20upx;">
						咨询费用:
					</view>
					<view class="text-price text-red">
						{{entrustObj.askMoney}}
					</view>
				</view>
				<view class="cu-list menu-avatar comment solids-top" style="margin-top: 0;">
					<view class="cu-item pic_title" v-for="(item,index) in entrustObj.yuUserNoticList" :key='item.id'>
						<view class="cu-avatar round" :style="{backgroundImage:'url('+ item.handlUrl +')'}"></view>
						<view class="content">
							<view class="text-grey">
								<view class="item_name">
									{{item.nickName}}
								</view>
								<view style="color: #FDCC15; margin-left: 10upx;" :class="item.isFloor!=1?'':'cuIcon-selectionfill'" class=" text-gray"></view>
							</view>
							<view class="text-gray text-content text-df">
								艾欧尼亚
							</view>
							<view class="padding-sm radius light " style="font-size: 30rpx;border-bottom: 1px solid #eee;padding-left: 0;">
								<view class="flex">
									<view class="flex-sub">
										<view class="cu-avatar round" :style="{backgroundImage:'url('+ item.handlUrl +')'}">

										</view>
										<text style="font-weight: bold;margin:0 10upx;">{{item.nickName}} <text :class="item.isFloor==1?'cuIcon-selectionfill text-yellow':''"></text>

										</text>
										<view @click="handConsult(item)" v-if="item.isFloor!=1" class="cu-tag bg-blue radius" style="color: #fff;margin-right: 10upx;">
											向他咨询
										</view>
										<!-- 	<text style="font-weight: bold;">{{entrustObj.nickName}}</text> -->
										: {{item.content}}
									</view>
								</view>
							</view>
							<view v-for="(item2,index) in item.newYuUserNoticList" :key='item2.id' class="padding-sm radius light  margin-top-sm"
							 style="font-size: 30rpx;border-bottom: 1px solid #eee;padding-left: 0;">
								<view class="flex">
									<view class="flex-sub">
										<view class="cu-avatar round" :style="{backgroundImage:'url('+ item2.handlUrl +')'}">

										</view>
										<text style="font-weight: bold;margin:0 10upx;">{{item2.nickName}}</text>

										<text :class="item2.isFloor==1?'cuIcon-selectionfill text-yellow':''"></text>
										<view @click="handConsult(item2)" v-if="item2.isFloor!=1" class="cu-tag bg-blue radius" style="color: #fff;margin-right: 10upx;">
											向他咨询
										</view>
										:
										{{item2.content}}
									</view>
								</view>
							</view>
							<view v-if="item.newYuUserNoticList.length<item.yuUserNoticList.length" @click="handMorm(item,index)" class="text-gray text-df"
							 style="margin: 20upx;text-align: center;width: 100%;">
								查看更多
							</view>
							<view v-if='item.newYuUserNoticList.length>=item.yuUserNoticList.length&&item.yuUserNoticList.length>=3' @click="handNoMorm(item,index)"
							 class="text-gray text-df" style="margin: 20upx;text-align: center;width: 100%;">
								收起
							</view>
							<view v-if="entrustObj.userId==userMsg.id || userMsg.identity==2" class="margin-top-sm flex justify-between">
								<view class="text-gray text-df">{{item.createTime}}</view>
								<view @click="answer(item)" class="text-gray text-df">
									回复
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>

		</view>
		<!-- <view class="sub_btn_box" v-if="userMsg.identity==2"> -->
		<view class="sub_btn_box">
			<view class="sub_pic">
			</view>
			<view class="btn-group ">
				<button class="cu-btn bg-red round shadow-blur" style="margin-left:20upx;" @click="handNewSub">立即回复</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		compare
	} from '../../../core/utit.js'
	export default {
		data() {
			return {
				isCard: true,
				entrustObj: {},
				replyList: [],
				userMsg: {},
				postid: '',
				newIndex: 3
			};
		},
		async onLoad(e) {
			if (this.postid == '') {
				this.postid = e.id
			}
			if (uni.getStorageSync('USER')) {
				this.userMsg = JSON.parse(uni.getStorageSync('USER'))
			}
			console.log(this.userMsg, 'userMsguserMsguserMsg')

			function diffTime(startDate, endDate) {

				startDate = new Date(startDate);

				endDate = new Date(endDate);

				var diff = endDate.getTime() - startDate.getTime(); //时间差的毫秒数 



				return diff / 1000;

			}
			let res = await this.http.getDetails(this.postid)
			console.log(res, this.userMsg)
			res.createTime = this.timestampToTime(res.createTime)
			res.yuUserNoticList.forEach(item => {
				item.createTime = this.timestampToTime(item.createTime)
				let newDeta = this.timestampToTime(new Date())
				console.log(diffTime(item.createTime, newDeta))

				if (item.yuUserNoticList != null) {

					// let nary = item.yuUserNoticList.sort(compare('parentId'))
					// console.log(nary,'narynarynary')
					// for (let i = 0; i < item.yuUserNoticList.length; i++) {
					// 	if (nary[i].id == nary[i + 1].parentId) {
					// 		nary[i].ceshi = nary[i + 1].nickName
					// 		nary[i].id2 = nary[i + 1].parentId
					// 	}
					// }
					// 	console.log("数组重复内容：" , nary);
					item.yuUserNoticList.sort(compare('parentId'))
					item.newYuUserNoticList = item.yuUserNoticList.slice(0, this.newIndex)
					item.newYuUserNoticList.sort(compare('parentId'))
				}

			})
			if(res.image){
				res.imgList = res.image.split(',')
			}
			
			this.entrustObj = res
		},
		async mounted() {

		},
		methods: {
			ViewImage(e) {
				uni.previewImage({
					urls: this.entrustObj.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			handConsult(item) {
				console.log(item)
				uni.showModal({
					title: '目前推出一对一免费咨询,后续将收费',
					success: async (res) => {
						if (res.confirm == true) {
							await this.http.saveChatRoom({
								lawId: item.userId,
								isOpen: 1
							})
							uni.showToast({
								title: '聊天室创建成功',
								icon: 'none'
							})
							uni.navigateTo({
								url: '/pages/message/myConsult/myConsult'
							})

						}


					}
				})
			},
			handNoMorm(row) {
				this.newIndex = 3
				row.newYuUserNoticList = row.yuUserNoticList.slice(0, this.newIndex)
			},
			handMorm(row, index) {
				console.log(row)
				this.newIndex = this.newIndex + 3
				row.newYuUserNoticList = row.yuUserNoticList.slice(0, row.newYuUserNoticList.length + 3)
			},
			answer(item) {
				let userID = JSON.parse(uni.getStorageSync('USER')).id
				let ifLouzhu = userID == this.entrustObj.userId ? '1' : '2'
				uni.navigateTo({
					url: '/pages/home/entrustDetails/answer?row=' + JSON.stringify(item) + "&ifLouzhu=" + ifLouzhu
				})
			},
			handNewSub() {
				let userID = JSON.parse(uni.getStorageSync('USER')).id
				console.log(userID, this.entrustObj.userId)
				let ifLouzhu = userID == this.entrustObj.userId ? '1' : '2'
				uni.navigateTo({
					url: '/pages/home/entrustDetails/answer?row=' + JSON.stringify(this.entrustObj) + "&isflg=1" + "&ifLouzhu=" +
						ifLouzhu
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #fff;
	}

	.pic_title {
		border-top: 0.5px solid #eee;
	}

	.sub_btn_box {
		width: 100%;
		height: 100upx;
		padding: 0 30upx;
		background: rgba(255, 255, 255, 1);
		display: flex;
		align-items: center;
		justify-content: space-between;
		position: fixed;
		bottom: 0;
		box-sizing: border-box;

		.btns {
			display: flex;

		}

		.sub_pic {
			.com_pic {
				color: #323233;
				font-size: 32upx;
				font-weight: bold;
			}

			.vip_pic {
				color: #E02020;
				font-size: 30upx;
				margin-top: 12upx;
			}
		}
	}
</style>
